package com.example.actionsignin.mvp.view;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.example.actionsignin.R;
import com.example.actionsignin.entity.DescribeEntity;
import com.example.actionsignin.entity.ImagesEntity;
import com.example.actionsignin.entity.LoginEntity;
import com.example.actionsignin.entity.UserEntity;
import com.example.actionsignin.http.HttpUtil;
import com.example.actionsignin.utils.GlideImageLoader;
import com.example.actionsignin.utils.ToastUtil;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * 活动详情
 */
public class DescribeActivity extends AppCompatActivity implements
        OnBannerListener,
        View.OnClickListener{

    private static final String TAG = "DescribeActivity";
    private String mAid = "";

    private TextView mActionName;
    private TextView mDate;
    private TextView mRepeat;
    private TextView mAddress;
    private TextView mPeople;
    private TextView mContent;

    private Banner banner;
    private List<String> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_describe);

        mAid = getIntent().getStringExtra("aid");
        if (mAid == null || mAid.equals(""))
        {
            finish();
        }
        Log.d(TAG, "onCreate: id " + mAid);

        initView();
        getData();
    }

    private void initBanner() {
        //图片加载器
        banner.setImageLoader(new GlideImageLoader());
        if (!this.isFinishing())
        {
            //图片集合
            banner.setImages(list);
        }

        //动画
        banner.setBannerAnimation(Transformer.DepthPage);
        banner.setIndicatorGravity(BannerConfig.CENTER);
        banner.start();
        banner.setOnBannerListener(this);
    }

    private void initView() {
        findViewById(R.id.activity_describe_left).setOnClickListener(this);
        findViewById(R.id.activity_describe_enroll).setOnClickListener(this);
        mActionName = (TextView) findViewById(R.id.action_describe_action_name);
        mPeople = (TextView) findViewById(R.id.action_describe_people);
        mDate = (TextView) findViewById(R.id.action_describe_date);
        mRepeat = (TextView) findViewById(R.id.action_describe_repeat);
        mAddress = (TextView) findViewById(R.id.action_describe_address);
        mContent = (TextView) findViewById(R.id.action_describe_content);
        banner = (Banner) findViewById(R.id.activity_describe_image);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();

        switch (id)
        {
            case R.id.activity_describe_left:
                finish();
                break;
            case R.id.activity_describe_enroll:
                doEnroll();
                break;
        }
    }

    private void getData()
    {
        HttpUtil.onSend().onActionDescribe(mAid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<DescribeEntity<UserEntity,ImagesEntity>>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull DescribeEntity<UserEntity,ImagesEntity> userEntityDescribeEntity) {
                        mActionName.setText(userEntityDescribeEntity.getName());
                        mDate.setText(userEntityDescribeEntity.getStart() + " - " + userEntityDescribeEntity.getEnd());
                        mRepeat.setText(userEntityDescribeEntity.getPeriod());
                        mAddress.setText(userEntityDescribeEntity.getAddress());
                        mContent.setText(userEntityDescribeEntity.getContent());
                        List<ImagesEntity> imagesEntity = userEntityDescribeEntity.getActivityImages();
                        for (int i = 0; i < imagesEntity.size(); i++) {
                            list.add(imagesEntity.get(i).getHttpPath());
                        }
                        if (userEntityDescribeEntity.getUser() != null)
                        {
                            mPeople.setText(userEntityDescribeEntity.getUser().getName());
                        }
                        Log.d(TAG, "onNext: " + userEntityDescribeEntity.getSigns().size());
                        for (int i = 0; i < userEntityDescribeEntity.getSigns().size(); i++) {

                            Log.d(TAG, "onNext: " + userEntityDescribeEntity.getSigns().get(i).getDate());
                            Log.d(TAG, "onNext: " + userEntityDescribeEntity.getSigns().get(i).getSign());
                            Log.d(TAG, "onNext: " + userEntityDescribeEntity.getSigns().get(i).getUser().getName());
                            Log.d(TAG, "onNext: " + userEntityDescribeEntity.getSigns().get(i).getUser().getSchool());
                            Log.d(TAG, "onNext: " + userEntityDescribeEntity.getSigns().get(i).getUser().getSnumber());
                            Log.d(TAG, "onNext: " + userEntityDescribeEntity.getSigns().get(i).getUser().getTelephone());
                        }

                        Log.d(TAG, "onNext: " + userEntityDescribeEntity.getSize());

                        Log.d(TAG, "onNext: " + userEntityDescribeEntity.getUsers().size());
                        for (int i = 0; i < userEntityDescribeEntity.getUsers().size(); i++) {

                            Log.d(TAG, "onNext: " + userEntityDescribeEntity.getUsers().get(i));
                            Log.d(TAG, "onNext: " + userEntityDescribeEntity.getUsers().get(i).getName());
                            Log.d(TAG, "onNext: " + userEntityDescribeEntity.getUsers().get(i).getSchool());
                            Log.d(TAG, "onNext: " + userEntityDescribeEntity.getUsers().get(i).getSnumber());
                            Log.d(TAG, "onNext: " + userEntityDescribeEntity.getUsers().get(i).getTelephone());
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.d(TAG, "onError: " + e);
                    }

                    @Override
                    public void onComplete() {
                        initBanner();
                    }
                });
    }

    private void doEnroll()
    {
        HttpUtil.onSend().onActionEnroll(mAid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<LoginEntity>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull LoginEntity loginEntity) {
                        if (loginEntity.getResult().equals("1"))
                        {
                            ToastUtil.INSTANCE(getApplicationContext(),"报名成功");
                        }else {
                            ToastUtil.INSTANCE(getApplicationContext(),"报名失败");
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        Log.d(TAG, "onError: " + e);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    protected void onPause() {
        super.onPause();
        banner.stopAutoPlay();
    }

    @Override
    protected void onResume() {
        super.onResume();
        banner.startAutoPlay();

    }

    @Override
    public void OnBannerClick(int position) {
        Intent intent = new Intent(DescribeActivity.this,LookPhotoActivity.class);
        intent.putExtra("path",list.get(position));
        startActivity(intent);
    }
}
